﻿using Microsoft.Extensions.Logging;
using SmartSql;
using SmartSql.Abstractions;
using SpiderPlatform.Entity;
using System;
using System.Collections.Generic;
using System.Linq;

namespace SpiderPlatform.Dao
{
    public class ItemDao
    {
        ISmartSqlMapper sqlMapper = null;
        public ItemDao()
        {
            var loggerFactory = new LoggerFactory();
            loggerFactory.AddDebug(minLevel: LogLevel.Debug);
            sqlMapper = MapperContainer.Instance.GetSqlMapper(new SmartSqlOptions
            {
                LoggerFactory = loggerFactory,
                ConfigPath = "SmartSqlMapMysql.xml"
            });
        }


        public List<ChannelItemEntity> QueryChannelItem(int channel_id)
        {
            var result = sqlMapper.Query<ChannelItemEntity>(new RequestContext
            {
                Scope = "Item",
                SqlId = "QueryChannelItem",
                Request = new
                {
                    channel_id                 
                }
            });
            return result.ToList();
        }
        public void CreateChannelItemHistory(List<ChannelItemHistoryEntity> hisList)
        {
            var result = sqlMapper.Execute(new RequestContext
            {
                Scope = "Item",
                SqlId = "CreateChannelItemHistory",
                Request = new
                {
                    hisList
                }
            });           
        }
        public void CreateChannelItem(List<ChannelItemEntity> itemList)
        {
            var result = sqlMapper.Execute(new RequestContext
            {
                Scope = "Item",
                SqlId = "CreateChannelItem",
                Request = new
                {
                    itemList
                }
            });
        }

        public void CreateChannelItemRealTime(List<ChannelItemHistoryEntity> itemList)
        {
            var result = sqlMapper.Execute(new RequestContext
            {
                Scope = "Item",
                SqlId = "CreateChannelItemRealTime",
                Request = new
                {
                    itemList
                }
            });
        }
        public List<ChannelItemHistoryEntity> QueryChannelItemRealTime(int channel_id)
        {
            var result = sqlMapper.Query<ChannelItemHistoryEntity>(new RequestContext
            {
                Scope = "Item",
                SqlId = "QueryChannelItemRealTime",
                Request = new
                {
                    channel_id
                }
            });
            return result.ToList();
        }
        
        public void UpdateChannelItemRealTime(List<ChannelItemHistoryEntity> itemList)
        {
            var result = sqlMapper.Execute(new RequestContext
            {
                Scope = "Item",
                SqlId = "UpdateChannelItemRealTime",
                Request = new
                {
                    itemList
                }
            });
        }
        public List<ApiData> QueryApiData()
        {
            var result = sqlMapper.Query<ApiData>(new RequestContext
            {
                Scope = "Item",
                SqlId = "QueryApiData"               
            });
            return result.ToList();
        }
        public void UpdateApiData(List<ApiData> itemList)
        {
            var result = sqlMapper.Execute(new RequestContext
            {
                Scope = "Item",
                SqlId = "UpdateApiData",
                Request= new {
                    itemList
                }
            });            
        }

        public void CreateProductTag(List<ProductTagInfo> itemList)
        {
            var result = sqlMapper.Execute(new RequestContext
            {
                Scope = "Item",
                SqlId = "CreateProductTag",
                Request = new
                {                  
                    itemList
                }
            });
        }

        public List<ProductTag> QueryChannelItemById(int rec_id)
        {
            var result = sqlMapper.Query<ProductTag>(new RequestContext
            {
                Scope = "Item",
                SqlId = "QueryChannelItemById",
                Request = new
                {
                    rec_id
                }
            });
            return result.ToList();
        }
        public List<ChannelItemHistoryEntity> QueryChannelItemBySku(int channel_id,List<string> list)
        {
            var result = sqlMapper.Query<ChannelItemHistoryEntity>(new RequestContext
            {
                Scope = "Item",
                SqlId = "QueryChannelItemBySku",
                Request = new
                {
                    channel_id,
                    channel_sku_list= list
                }
            });
            return result.ToList();
        }
        

    }
}
